﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Anthem;
using spUtils;
using spMapping;
using spData.Common;


namespace demo.Page.HT
{
    public partial class ht_Mzbht : TaskPage
    {
        #region Property
        public string isCssShow
        {
            get { return ViewState["isCssShow"] == null ? "" : (string)ViewState["isCssShow"]; }
            set { ViewState["isCssShow"] = value; }
        }
        public List<dc_ht_Mzbht_Details> details
        {
            get { return ViewState["details"] == null ? new List<dc_ht_Mzbht_Details>() : ViewState["details"] as List<dc_ht_Mzbht_Details>; }
            set { ViewState["details"] = value; }
        }
        //初始化下拉
        spWeb.DDLBind db = new spWeb.DDLBind();
        #endregion

        #region Method Init Controls Value & Show/Hide & Enable/Disable & Opinion
        private void InitControls()
        {
            #region Init controls value
            //绑定城市
            db.BindCity(City);
            Expression exp = new Expression();
            string did = Request.QueryString["DID"];
            exp = string.IsNullOrEmpty(did) ? dc_ht_Mzbht.process == TaskAPI.Process & dc_ht_Mzbht.incident == TaskAPI.Incident : dc_ht_Mzbht.id == did;
            if (TaskAPI.IsInitiate)
            {
                exp &= dc_ht_Mzbht.userName == USER.UserName;
            }

            dc_ht_Mzbht v = exp.ListFirst<dc_ht_Mzbht>();
            if (v != null)
            {
                SetControlsValue(v);
                DepEName.Text = v.Department;
                //DID方式加载
                if (!string.IsNullOrEmpty(did))
                {
                    TaskAPI.LoadByDID(v.Process, v.Incident);
                    Process = v.Process;
                    Incident = v.Incident;
                }
            }
            else
            {
                UserFullName.Text = USER.UserFullName;
                Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                Department.Text = USER.Department;
                DepEName.Text = USER.DepEName;
                Tel.Text = USER.EXTEND.CellPhone;
            }
            BindDetails();
            PushKjkm();

            #endregion

            #region Init Show/Hide、Enable/Disable
            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            //bool flg = true;
            SetEnable(flg, Subject, City, GSGS, XMMC, HTQDDW, MZBMC, MZBLX, MZBLX2, MZBLX3, MZBLY, HTMZ, DFDW, HTLX, HTLX2, HTLX3, HTJE, HTJEDX, ZBJBL, ZBJ, HTZYNR, HTJBSY, HTJGTK, HTJDTK, FKJDSFYZ, FKJDTZLY, CBKM, MBJEDWZ, MBJEZZ, MBJEZZDX, YQYJSJE, YQYJSJEDX, YFSFHTJE, YFSFHTJEDX, HCJBR, YMBCBCEJSM, YJHXFSCBMX);

            SetVisible(flg, R_Subject, R_City, R_GSGS, R_XMMC, R_Details);
            #endregion
        }
        #endregion

        #region Method Data Validation
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            if (!AssExt.IsConferWithBack && !AssExt.IsForwardWithBack)
            {
                if (TaskAPI.IsBeginStep)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, City.Text, "“城市公司”为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, GSGS.Text, "“开发公司”为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, XMMC.Text, "“项目名称及分期”为必填项!"));
                    regs.Add(new Regular(RegularExp.Float, HTJE.Text, "“合同金额”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, MBJEDWZ.Text, "“目标金额单位值”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, MBJEZZ.Text, "“目标金额总值”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, YQYJSJE.Text, "“已签约/结算金额”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, YFSFHTJE.Text, "“已发生非合同金额”应为数字!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, details.Count > 0 ? "!" : "", "“合同支付条款”应为必填项!"));
                }
            }
            return Validation(regs);
        }
        #endregion

        #region Method Engine Transfer Value
        private void EngineTransferValue()
        {
            if (TaskAPI.IsBeginStep)
            {
                TaskAPI.Summary = Subject.Text;
                spWeb.WebHelper.IntSetValue(this.TaskAPI, GSGS.SelectedValue, XMMC.SelectedValue);
            }
            TaskAPI["e_Approve"] = AssExt.Assessment.Opinion;
        }

        #endregion

        #region Method Submit
        private bool Submit(bool IsSend)
        {
            #region “开始”步骤
            if (TaskAPI.IsBeginStep)
            {
                dc_ht_Mzbht v = (dc_ht_Mzbht.process == TaskAPI.Process & dc_ht_Mzbht.incident == TaskAPI.Incident
                                & dc_ht_Mzbht.userName == (TaskAPI.IsInitiate ? USER.UserName : TaskAPI.UserName)).ListFirst<dc_ht_Mzbht>();
                bool result = GetControlsValue<dc_ht_Mzbht>(ref v);
                if (result)
                {
                    v.Process = TaskAPI.Process;
                    v.Incident = TaskAPI.Incident;
                    v.UserName = USER.UserName;
                    v.UserFullName = USER.UserFullName;
                    v.Time = System.DateTime.Now;
                    ApplicationNo.Text = GetNewSequence("DCHT-MZBHT-" + DateTime.Now.ToString("yyyyMMdd") + "-", "", 3);
                    v.ApplicationNo = ApplicationNo.Text;
                }
                ORM.Save(v);

                (dc_ht_Mzbht_Details.process == TaskAPI.Process & dc_ht_Mzbht_Details.incident == TaskAPI.Incident & dc_ht_Mzbht_Details.userName == USER.UserName).Edit<dc_ht_Mzbht_Details>(o => o.Status = 0);

                CollectKjkm();
                //loop for save
                foreach (dc_ht_Mzbht_Details detail in details)
                {
                    detail.Process = TaskAPI.Process;
                    detail.Incident = TaskAPI.Incident;
                    detail.UserName = USER.UserName;
                    detail.Time = System.DateTime.Now;
                    detail.Status = 1;
                    ORM.Save(detail);
                }
            }
            #endregion


            if (IsSend)
            {
                EngineTransferValue();
                ORM.Save(AssExt.Assessment);
                TaskAPI.Send();
            }

            if (IsSend && TaskAPI.IsInitiate)
            {
                (dc_ht_Mzbht.process == TaskAPI.Process & dc_ht_Mzbht.incident == 0 & dc_ht_Mzbht.userName == USER.UserName).Edit<dc_ht_Mzbht>(p => p.Incident = TaskAPI.Incident);
                (dc_ht_Mzbht_Details.process == TaskAPI.Process & dc_ht_Mzbht_Details.incident == 0 & dc_ht_Mzbht_Details.userName == USER.UserName).Edit<dc_ht_Mzbht_Details>(p => { p.Incident = TaskAPI.Incident; });
            }

            //退出
            if (IsSend)
            {
                Exit();
            }
            return true;
        }
        #endregion

        #region Event Page
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region Event Button Click

        #region Event Save As Draft
        protected void SaveAsDraft_Click(object sender, EventArgs e)
        {
            bool success = Submit(false);
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}')", success ? "保存成功!" : "保存失败!"));
        }
        #endregion

        #region Event Send
        protected void Send_Click(object sender, EventArgs e)
        {
            List<string> msg = DataValidation();
            if (msg.Count > 0)
            {
                Anthem.Manager.AddScriptForClientSideEval(Alert(msg));
                return;
            }

            bool success = Submit(true);
            (sender as spWeb.Controls.AssessmentExt).Refresh();
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}');{1}", success ? "提交成功!" : "提交失败!", success ? "window.close();" : ""));
        }
        #endregion

        #region Event Aborted
        protected void Aborted_Click(object sender, EventArgs e)
        {
            (dc_ht_Mzbht.process == TaskAPI.Process & dc_ht_Mzbht.incident == TaskAPI.Incident).Edit<dc_ht_Mzbht>(p => { p.Status = 4; });
        }
        #endregion

        #endregion

        #region Details Bind & Add, Delete & CollectKjkm, PushKjkm

        #region Method BindDetails
        private void BindDetails()
        {
            Expression exp = (dc_ht_Mzbht_Details.process == TaskAPI.Process & dc_ht_Mzbht_Details.incident == TaskAPI.Incident & dc_ht_Mzbht_Details.status == 1);

            details = exp.List<dc_ht_Mzbht_Details>(new Region(0, 100), dc_ht_Mzbht_Details.id.Asc) as List<dc_ht_Mzbht_Details>;
            if (details.Count == 0 & TaskAPI.IsBeginStep)
            {
                for (int i = 0; i < 3; i++)
                {
                    details.Add(new dc_ht_Mzbht_Details());
                }
            }
            Details.DataSource = details;
            Details.DataBind();
        }

        protected void Details_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //null
        }

        #endregion

        #region Event Row Add, Delete
        protected void AddDelete_Click(object sender, EventArgs e)
        {
            //收集数据
            CollectKjkm();

            if ((sender as Anthem.Button).ID == "detailsDel")
            {
                foreach (RepeaterItem ri in Details.Items)
                {
                    Anthem.CheckBox current = ri.FindControl("Cb_XZ") as Anthem.CheckBox;
                    if (current.Checked)
                    {
                        int index = spWeb.CommHelper.ConvertToInt(current.Attributes["alt"]);
                        if (index != 0)
                        {
                            for (int i = 0; i < details.Count; i++)
                            {
                                if (details[i].No == index)
                                {
                                    details.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                details.Add(new dc_ht_Mzbht_Details());
            }

            //重新编号、绑定
            for (int i = 0; i < details.Count; i++)
            {
                details[i].No = i;
            }
            Details.DataSource = details;
            Details.DataBind();

            PushKjkm();
        }
        #endregion

        #region Method CollectKjkm, PushKjkm
        protected void CollectKjkm()
        {
            int i = 0;
            foreach (RepeaterItem ri in Details.Items)
            {
                Anthem.DropDownList PayOrder = ri.FindControl("PayOrder") as Anthem.DropDownList;
                Anthem.DropDownList PayType = ri.FindControl("PayType") as Anthem.DropDownList;
                Anthem.TextBox PayRemarks = ri.FindControl("PayRemarks") as Anthem.TextBox;

                details[i].PayOrder = PayOrder.SelectedItem.Text;
                details[i].PayType = PayType.SelectedItem.Text;
                details[i].PayRemarks = PayRemarks.Text;
                i++;
            }
        }

        protected void PushKjkm()
        {
            int i = 0;
            bool isModifiy = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            //bool isModifiy = true;
            foreach (RepeaterItem ri in Details.Items)
            {
                Anthem.DropDownList PayOrder = ri.FindControl("PayOrder") as Anthem.DropDownList;
                Anthem.DropDownList PayType = ri.FindControl("PayType") as Anthem.DropDownList;
                Anthem.TextBox PayRemarks = ri.FindControl("PayRemarks") as Anthem.TextBox;

                SetEnable(isModifiy, PayOrder, PayType, PayRemarks);
                isCssShow = isModifiy ? "" : "display:none;";
                i++;
            }
        }
        #endregion

        #region Event City(城市公司) & Gsgs(开发公司)
        protected void City_TextChanged(object sender, EventArgs e)
        {
            db.BindGsgs(GSGS, City.SelectedValue);
        }
        protected void Gsgsbmkfgs_TextChanged(object sender, EventArgs e)
        {
            db.BindXmmc(XMMC, GSGS.SelectedValue);
        }
        #endregion

        #region (大写转换)
        protected void HTJE_TextChanged(object sender, EventArgs e)
        {
            HTJEDX.Text = Utils.TransformNumberToChina(HTJE.Text.Replace(",", ""));
        }
        protected void YQYJSJE_TextChanged(object sender, EventArgs e)
        {
            YQYJSJEDX.Text = Utils.TransformNumberToChina(YQYJSJE.Text.Replace(",", ""));
        }
        protected void YFSFHTJE_TextChanged(object sender, EventArgs e)
        {
            YFSFHTJEDX.Text = Utils.TransformNumberToChina(YFSFHTJE.Text.Replace(",", ""));
        }
        protected void MBJEZZ_TextChanged(object sender, EventArgs e)
        {
            MBJEZZDX.Text = Utils.TransformNumberToChina(MBJEZZ.Text.Replace(",", ""));
        }
        #endregion

        #endregion
    }
}